Basic Operators in R

Zulquar Nain
AMU

February 15, 2024

Lets learn 4m History

  • Open R Studio using R project(created before)

  • Create a R script file

  • save it(Use lecture number or date as name)

  • Do make comments in your script file

  • In the r script file/console, calculate \(21+21\) , \(7*6\) and \((0.3/4) × \sqrt(31360)\)

Solutions

The Code
21+21
[1] 42
The Code
7*6
[1] 42
The Code
.3/4*sqrt(31360)
[1] 13.28157

Basic Operators

  • Basic Operators can be classified into four categories
    • Arithmetic Operators
    • Relational Operators
    • Logical Operators
    • Assignment Operators

Assignment Operator

  • These operators are used to assign values to variables

  • The operators <-and = can be used most of the time interchangeably

  • The <<- operator is used for assigning to variables in the parent environments (more like global assignments)

  • right ward operators, however rarely used

Table: Assignment Operators
Operator Descriptor
<-, <<-, = Leftward Assignemnt
->, ->> Rightward Assignment

Assignment Operator

Example

##| code-line-numbers: "1|3|4"

x <- 5
x
[1] 5
y = 9
y
[1] 9
10 -> b
b
[1] 10
M <<- 50
50 ->> N
M > N
[1] FALSE
msg <- "Hey, YOU people are SMART! & I mean it"
msg
[1] "Hey, YOU people are SMART! & I mean it"

Arithmetic Operators

Table 2: Arithmatic Operators
Operators Description
+ Addition
- Subtraction
* Multiplication
/ Division
^ Exponential
%% Modulus(Remainder Division)

Arithmetic Operator

Example

##| code-line-numbers: "1|3|4"

X <- 5 # this is assignment operator, we discussed earlier
Y <- 16 # we have values of X and Y

X+Y
[1] 21
X-Y
[1] -11
X*Y
[1] 80
Y/X
[1] 3.2
Y%%X
[1] 1
Y^X
[1] 1048576
2^3
[1] 8

Relational Operators

  • Relational operators are used to compare between values

    Table 3: Relational Operator
    Operators Description
    < Less than
    > Greater than
    <= Less than or equal to
    >= Greater than or equal to
    == Equal to
    != Not equal to

Relational Operators

Examples

##| code-line-numbers: "1|3|4"

X <- 5 # this is assignment operator, we discussed earlier
Y <- 16 # we have values of X and Y
X<Y
[1] TRUE
X>Y
[1] FALSE
X<=5
[1] TRUE
Y>20
[1] FALSE
Y==16
[1] TRUE
Y!=X
[1] TRUE

Logical Operators

  • Logical operators are used to carry out Boolean operations like AND, OR etc

  • Operators & and | perform element-wise operation producing result having length of the longer operand

  • But && and || examines only the first element of the operands resulting into a single length logical vector

  • Zero is considered FALSE and non-zero numbers are taken as TRUE

Logical Operators

Logical operators

  • &&→(logical AND)

  • Compares two expressions and returns true if both evaluate to true.

  • Returns false only if both expressions are false.

    • true && false → Evaluates false because the second is false

    • false && true → Evaluates false because the first is false

    • true && true → Evaluates true because both are true

    • false && false → Evaluates false because both are false

Logical Operators-Explanations

  • ||→(logical OR)

  • Compares two expressions and returns true if one or both evaluate to true.

  • Returns false only if both expressions are false.

    • true || false → Evaluates true because the first is true

    • false || true → Evaluates true because the second is true

    • true || true → Evaluates true because both are true

    • false || false → Evaluates false because both are false

Logical Operators-Examples

##| code-line-numbers: "1|2|3|4|5|6|7"

X <- c(TRUE, FALSE, 0,6)
Y <- c(FALSE,TRUE,FALSE,TRUE)
!X
[1] FALSE  TRUE  TRUE FALSE
X&Y
[1] FALSE FALSE FALSE  TRUE
X|Y
[1]  TRUE  TRUE FALSE  TRUE
a <- c(TRUE)
b <- c(FALSE)
a&&b
[1] FALSE
a||b
[1] TRUE

Explore!

age <- 16
if (!(age > 18)) {
  print("You are Too Young")
} else if(age > 18 && age <= 35) {
  print("Young Guy")
} else if(age == 36 || age <= 60) {
  print("You are Middle Age Person")
} else {
  print("You are too Old")
}
[1] "You are Too Young"

Some more basic operators

Your TURN

  • Add, subtract, multiply and divide your age with your friend’s age
  • Use logical operators to compare your age with your friend’s age
  • If the value of x in the last example is changeded to 20, 40 and 66, what will be the results?

Have patience